from PyQt5.QtSql import QSqlQuery
from PyQt5.QtSql import QSqlQuery
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QDialog, QVBoxLayout, QMessageBox

from Utils import openDB


def getTreeData(modelName, fatherID='无父组件'):
    db = openDB()
    query = QSqlQuery()
    sql = "Select ComponentID, ComponentNO, ComponentName, ComponentType, Count, LifeType, LifeTime From T_Component Where ModelName = '%s' and FatherID = '%s'" % (
    modelName, fatherID)
    # print(sql)
    query.exec(sql)
    list = []
    while (query.next()):
        list.append([query.value(i) for i in range(7)])
    return list


def setTreeLevel(modelName, current_level, root):
    for i in range(len(current_level)):
        levelOne = QTreeWidgetItem(root)
        for j in range(1, 7):
            levelOne.setText(j - 1, str(current_level[i][j]))
        second_level = getTreeData(modelName=modelName, fatherID=current_level[i][0])
        if second_level:
            setTreeLevel(modelName, second_level, levelOne)


class TreeWidget(QDialog):
    def __init__(self, plist, init_level=None, ttype=None):
        super(TreeWidget, self).__init__()
        self.setWindowTitle('TreeWidget')
        self.tree = QTreeWidget()  # 实例化一个TreeWidget对象
        self.tree.setColumnCount(6)  # 设置部件的列数为2
        self.tree.setHeaderLabels(['产品/组件代号', '产品/组件名称', '类型', '数量(件|个)', '寿命类型', '寿命(天|次)'])  # 设置头部信息对应列的标识符
        self.setFixedHeight(800)
        self.setFixedWidth(1400)

        self.tree.setColumnWidth(0, 200)
        self.tree.setColumnWidth(1, 200)
        self.tree.setColumnWidth(2, 200)
        self.tree.setColumnWidth(3, 200)
        self.tree.setColumnWidth(4, 200)

        self.tree.setColumnWidth(5, 200)
        self.tree.setColumnWidth(6, 200)

        root = QTreeWidgetItem(self.tree)
        if ttype == 'component':
            modelName = plist[1]
            first_level = init_level
            root.setText(0, str(plist[2]))
            root.setText(1, str(plist[1]))
            root.setText(2, str(plist[5]))
            root.setText(3, str(plist[6]))
            root.setText(4, str(plist[7]))
            root.setText(5, str(plist[8]))
        elif ttype == 'product':
            first_level = getTreeData(plist[2])
            modelName = plist[2]
            productCount = 1
            # 设置root为self.tree的子树，故root是根节点
            # self.tree.setHeaderLabels(['产品/组件代号', '产品/组件名称', '类型', '数量(件|个)', '寿命类型', '寿命(天|次)'])  # 设置头部信息对应列的标识符
            root.setText(0, str(plist[3]))  # 设置根节点的名称
            root.setText(1, str(plist[2]))
            root.setText(2, '产品')
            root.setText(3, str(productCount))
            root.setText(4, str(plist[14]))
            root.setText(5, str(plist[15]))
        else:
            first_level = getTreeData(plist[2])
            modelName = plist[2]
            root.setText(0, str(plist[2]))  # 设置根节点的名称
            root.setText(1, str(plist[1]))
            root.setText(2, '产品')
            root.setText(3, str(1))
            root.setText(4, str(plist[3]))
            root.setText(5, str(plist[4]))

        if first_level:
            self.isNone = False
            setTreeLevel(modelName, first_level, root)
        else:
            QMessageBox.information(QDialog(), "提示！", "该产品暂无其他组件，请为其添加后查看！", QMessageBox.Yes, QMessageBox.Yes)
            self.isNone = True

        # 为root节点设置子结点
        # child1 = QTreeWidgetItem(root)
        # child1.setText(0, 'child1')
        # child1.setText(1, 'name1')
        # child2 = QTreeWidgetItem(root)
        # child2.setText(0, 'child2')
        # child2.setText(1, 'name2')
        # child3 = QTreeWidgetItem(root)
        # child3.setText(0, 'child3')
        # child4 = QTreeWidgetItem(child3)
        # child4.setText(0, 'child4')
        # child4.setText(1, 'name4')

        self.tree.addTopLevelItem(root)
        layout = QVBoxLayout()
        layout.addWidget(self.tree)
        self.setLayout(layout)
        self.tree.expandAll()


class TreeWidget1(QDialog):
    def __init__(self, plist, init_level=None, ttype=None):
        super(TreeWidget1, self).__init__()
        self.setWindowTitle('TreeWidget')
        self.tree = QTreeWidget()  # 实例化一个TreeWidget对象
        self.tree.setColumnCount(6)  # 设置部件的列数为2
        self.tree.setHeaderLabels(['产品/组件代号', '产品/组件名称', '类型', '数量(件|个)', '寿命类型', '寿命(天|次)'])  # 设置头部信息对应列的标识符
        self.setFixedHeight(800)
        self.setFixedWidth(1400)

        self.tree.setColumnWidth(0, 200)
        self.tree.setColumnWidth(1, 200)
        self.tree.setColumnWidth(2, 200)
        self.tree.setColumnWidth(3, 200)
        self.tree.setColumnWidth(4, 200)

        self.tree.setColumnWidth(5, 200)
        self.tree.setColumnWidth(6, 200)

        root = QTreeWidgetItem(self.tree)
        if ttype == 'component':
            modelName = plist[1]
            first_level = init_level
            root.setText(0, str(plist[2]))
            root.setText(1, str(plist[1]))
            root.setText(2, str(plist[5]))
            root.setText(3, str(plist[6]))
            root.setText(4, str(plist[7]))
            root.setText(5, str(plist[8]))
        elif ttype == 'product':
            first_level = getTreeData(plist[2])
            modelName = plist[2]
            productCount = 1
            # 设置root为self.tree的子树，故root是根节点
            # self.tree.setHeaderLabels(['产品/组件代号', '产品/组件名称', '类型', '数量(件|个)', '寿命类型', '寿命(天|次)'])  # 设置头部信息对应列的标识符
            root.setText(0, str(plist[3]))  # 设置根节点的名称
            root.setText(1, str(plist[2]))
            root.setText(2, '产品')
            root.setText(3, str(productCount))
            root.setText(4, str(plist[14]))
            root.setText(5, str(plist[15]))
        else:
            first_level = getTreeData(plist[2])
            modelName = plist[2]
            root.setText(0, str(plist[2]))  # 设置根节点的名称
            root.setText(1, str(plist[1]))
            root.setText(2, '产品')
            root.setText(3, str(1))
            root.setText(4, str(plist[3]))
            root.setText(5, str(plist[4]))

        if first_level:
            self.isNone = False
            setTreeLevel(modelName, first_level, root)
        else:
            QMessageBox.information(QDialog(), "提示！", "该产品暂无其他组件，请为其添加后查看！", QMessageBox.Yes, QMessageBox.Yes)
            self.isNone = True

        # 为root节点设置子结点
        # child1 = QTreeWidgetItem(root)
        # child1.setText(0, 'child1')
        # child1.setText(1, 'name1')
        # child2 = QTreeWidgetItem(root)
        # child2.setText(0, 'child2')
        # child2.setText(1, 'name2')
        # child3 = QTreeWidgetItem(root)
        # child3.setText(0, 'child3')
        # child4 = QTreeWidgetItem(child3)
        # child4.setText(0, 'child4')
        # child4.setText(1, 'name4')

        self.tree.addTopLevelItem(root)
        layout = QVBoxLayout()
        layout.addWidget(self.tree)
        self.setLayout(layout)
        self.tree.expandAll()
